// packageE/cube/cube.js
const app = getApp();
Component({
  properties: {
    datas: {
      type: null,
    },
    component_id: {
      type: null,
    },
  },
  // 私有数据，可用于模板渲染
  data: {
    emptyImage:  `${app.globalData.imgPath}image.png`,

    cubeWidth: 375, //魔方宽度
    cubeHeight: 375, //魔方高度
    cudeSelecting: {
      start: null,
      end: null,
      data: [],
    },
    cudeSelected: [], //已经生成的单元
    havePicture: false,

    cubeX: 1,
    cubeItemWidth: "",
    cubeItemHeight: "",
  },

  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {},
    moved() {},
    detached() {},
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() {},
  // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {
    this.checkhavePicture();
    this.setData({
      cubeWidth: wx.getSystemInfoSync().windowWidth,
    });

    this.setData({
      cubeItemWidth: this.data.cubeWidth / this.data.datas.picture.density,
      cubeItemHeight: this.data.cubeWidth / this.data.datas.picture.density,
    });
  },

  pageLifetimes: {
    // 组件所在页面的生命周期函数
    show() {},
    hide() {},
    resize() {},
  },
  methods: {
    goimgurl(e) {
      let item = e.currentTarget.dataset.item.value;
      if (item.is_minApp == 2) {
        if (item.gh_id) {
          if (item.minApp_link) {
            try {
              wx.navigateToMiniProgram({
                appId: item.gh_id,
                path: item.minApp_link,
                extraData: {},
                envVersion: "release",
                success(res) {
                  // 打开成功
                },
                fail(res) {
                  wx.showToast({
                    title: "小程序跳转失败",
                    icon: "none",
                    duration: 2000,
                  });
                },
              });
            } catch (e) {
              wx.showToast({
                title: "小程序跳转失败",
                icon: "none",
                duration: 2000,
              });
            }
          } else {
            wx.showToast({
              title: "请设置跳转小程序后的跳转链接",
              icon: "none",
              duration: 2000,
            });
          }
        } else {
          wx.showToast({
            title: "请设置小程序所需跳转appId",
            icon: "none",
            duration: 2000,
          });
        }
      } else {
        let url = item.h5_link;
	      let value = wx.getStorageSync("yz_uid");
	      let customParams = { mid: value };
        try {
	        if(url.indexOf("pages/live-player-plugin") > 0) {
		        url = url + `&open_share_ticket=1&custom_params=${encodeURIComponent(JSON.stringify(customParams))}`;
	        }
          wx.navigateTo({
            url: url,
          });
        } catch (e) {
          wx.showToast({
            title: "链接设置错误",
            icon: "none",
            duration: 2000,
          });
        }
      }
    },
    checkhavePicture() {
      if (this.data.datas.picture) {
        this.setData({
          havePicture: false,
          cubeX: 1,
        });
        for (let i = 0; i < this.data.datas.picture.cudeSelected.length; i++) {
          if (this.data.datas.picture.cudeSelected[i].end.x > this.data.cubeX) {
            this.setData({
              cubeX: this.data.datas.picture.cudeSelected[i].end.x,
            });
          }
          if (this.data.datas.picture.cudeSelected[i].value.image) {
            this.setData({
              havePicture: true,
            });
          }
        }

        if (
          this.data.datas.picture.cudeSelected.length === 0 ||
          !this.data.havePicture
        ) {
          this.setData({
            cubeX: 4,
          });
        }
      }
    },
  },
});
